/**
 * @author ChaP
 * @date 2019/05/18
 * @describe:
 */
package CodingTest.AC20190518;

import java.util.Stack;

public class addTwoNumbers {
    class ListNode{
        int val;
        ListNode next;
        ListNode(int x){val = x;}
    }
    public ListNode addTwoNumbers(ListNode l1,ListNode l2){
        Stack<ListNode> s1 = new Stack<ListNode>();
        Stack<ListNode> s2 = new Stack<ListNode>();
        ListNode res = new ListNode(-1) ;
        ListNode dummp = res;
        int len1 = s1.size();
        int len2 = s2.size();
        while (l1.next!= null){
            s1.push(l1);
            l1 = l1.next;
        }
        s1.push(l1);
        while (l2.next!= null){
            s2.push(l2);
            l2 = l2.next;
        }
        s1.push(l2);
        int carry = 0;
        if(len1>=len2){
            while(!s2.isEmpty()){
                int s = s1.pop().val + s2.pop().val + carry;
                res.next = new ListNode(s%10);
                carry = s/10;
                res = res.next;
            }
            while(!s1.isEmpty()){
                int s = s1.pop().val+carry;
                res.next = new ListNode(s%10);
                carry = s/10;
                res = res.next;
            }
        }else{
            while(!s1.isEmpty()){
                int s = s1.pop().val + s2.pop().val + carry;
                res.next = new ListNode(s%10);
                carry = s/10;
                res = res.next;
            }
            while(!s2.isEmpty()){
                int s = s2.pop().val+carry;
                res.next = new ListNode(s%10);
                carry = s/10;
                res = res.next;
            }
        }




return res;

    }
}
